package objects.info;

import java.util.Calendar;
import java.util.Random;

import objects.ObjectCodes;

import com.google.gson.Gson;

import utils.CommandStrings;
import utils.Shell;
import utils.Utils;

public class ClusterStatusInfo implements CommandStrings, ObjectCodes{
	public String time;
	public String date;
	public int freenodes;
	public int freeprocs;
	public int activejobs;
	public int queuedjobs;
	
	public ClusterStatusInfo()
	{
		date = new java.sql.Date(new java.util.Date().getTime()).toString();
		time = new java.sql.Time(new java.util.Date().getTime()).toString();
		if(Utils.MOCK) {
			Random randomGenerator = new Random();
			activejobs = randomGenerator.nextInt(20);
			queuedjobs = -1;
			freenodes = randomGenerator.nextInt(20);
			freeprocs = randomGenerator.nextInt(20);
			return;
		}
		try{
			String [] dat = Shell.runOneCmd();
			activejobs = Integer.parseInt(dat[0]);
			queuedjobs = Integer.parseInt(dat[1]);
			String nodes = dat[3];
			freenodes = Integer.parseInt(nodes.split("/")[1]) - Integer.parseInt(nodes.split("/")[0]);
			String procs = dat[2];
			freeprocs = Integer.parseInt(procs.split("/")[1]) - Integer.parseInt(procs.split("/")[0]);
		}catch(Exception e){activejobs=0;freenodes=0;freeprocs=0;queuedjobs=0;}
	}
	
	public String toString()
	{
		return CLU_ST_INFO+"|"+ new Gson().toJson(this);
	}
	public static ClusterStatusInfo fromJSon(String json)
	{
		return new Gson().fromJson(json,ClusterStatusInfo .class);
	}
}
